package dilog;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import model.UserArticle;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;
/**
 * 自定义退出弹出框
 * @author qinfan
 *
 * 2015-11-6
 */
public class RewardsDialog {
	private Activity activity;
	private AlertDialog dialog;
	private String title;
	private int num;
	private int articleId;

	public RewardsDialog(Activity activity,String title,int articleId) {
		this.activity = activity;
		this.title = title;
		this.articleId = articleId;
	}
	public void showDialog(){
		dialog=new AlertDialog.Builder(activity).create();
		//点击外部区域不能取消dialog 
		dialog.setCanceledOnTouchOutside(false);
		dialog.setOnKeyListener(keylistener);
		dialog.show();

		Window window = dialog.getWindow();
		window.setContentView(R.layout.reward_dialog_item);
		TextView tv_title = (TextView) window.findViewById(R.id.dialog_title);
		mEtNum = (EditText) window.findViewById(R.id.et_num);
		tv_title.setText(title);
		TextView tv_confirm = (TextView) window.findViewById(R.id.tv_confirm);
		TextView tv_cancel = (TextView) window.findViewById(R.id.tv_cancel);
		tv_confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Rewards();
				dialog.dismiss();
			}
		});

		tv_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});

		window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		window.setGravity(Gravity.CENTER);
	}

	protected void Rewards() {
			if(Integer.parseInt(mEtNum.getText().toString().trim())>=100){
				Toast.makeText(activity, "提示:打赏额度为100", 1).show();
			}else {
				Map<String, String> params=new HashMap<String,String>();
				params.put("user_id", ""+SetUtils.GetUserId(activity));
				params.put("article_id", ""+articleId);
				params.put("score", ""+Integer.parseInt(mEtNum.getText().toString().trim()));
//				Toast.makeText(activity, ""+SetUtils.GetUserId(activity)+"articleId:"+articleId+"score"+Integer.parseInt(mEtNum.getText().toString().trim()), 1).show();
				HTTPUtils.post(activity, Constance.URL.REWARDS, params, new VolleyListener() {
					@Override
					public void onResponse(String arg0) {
//					 Toast.makeText(activity, arg0.toString(), 1).show();
						UserArticle dateStatus = GsonUtils.parseJSON(arg0, UserArticle.class);
						if(dateStatus.getStatus().equals(Constance.STATUS_TYPE.SUCCESS)){
							Toast.makeText(activity, "恭喜您，打赏成功", 1).show();
						}else {
							Toast.makeText(activity, "抱歉，您的积分不足！", 1).show();
						}
					}
					@Override
					public void onErrorResponse(VolleyError arg0) {
						Toast.makeText(activity,  arg0.toString(), 1).show();
					}
				});
			}
	}

	public static OnKeyListener keylistener = new DialogInterface.OnKeyListener(){
		public boolean onKey(DialogInterface dialog,  int keyCode, KeyEvent event) {
			if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	} ;
	private EditText mEtNum;
}
